﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WP7PhoneTracker.EntityClasses;

namespace WP7PhoneTracker.Helpers
{
    public class CrudTracking
    {
        internal static Boolean AddTracking(Tracking tracking)
        {
            try
            {
                using (var context = new TrackerDataContext(App.ConnectionString))
                {
                    var res = (from r in context.Trackings
                                    where r.Latitude == tracking.Latitude 
                                    && r.Longitude == tracking.Longitude
                                    select r).FirstOrDefault();
                    if (res == null)
                    {
                        //Add a new tracking to the context
                        context.Trackings.InsertOnSubmit(tracking);
                        context.SubmitChanges(); 
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        internal static IList<Tracking> GetTrackings()
        {
            try
            {
                IList<Tracking> trackings = null;
                using (var context = new TrackerDataContext(App.ConnectionString))
                    trackings = (from c in context.Trackings select c).ToList();
                return trackings;
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                return null;
            }
        }

        internal static Boolean UpdateTracking(Tracking tracking)
        {
            try
            {
                using (var context = new TrackerDataContext(App.ConnectionString))
                {
                    Tracking res = (from r in context.Trackings
                                    where r.Id == tracking.Id
                                    select r).FirstOrDefault();

                    if (res != null)
                    {
                        res.LocationName = tracking.LocationName;
                        res.CityName = tracking.CityName;
                        res.CountryName = tracking.CountryName;

                        context.SubmitChanges();
                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                return false;
            }
        }

        private Boolean DeleteTracking(Tracking tracking)
        {
            try
            {
                using (var context = new TrackerDataContext(App.ConnectionString))
                {
                    context.Trackings.DeleteOnSubmit(tracking);
                    context.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        internal static Boolean DeleteAllTracking(IEnumerable<Tracking> trackings)
        {
            try
            {
                using (var context = new TrackerDataContext(App.ConnectionString))
                {
                    foreach (var tracking in trackings)
                    {
                        var res = (from dt in context.Trackings where dt.Id == tracking.Id select dt).Single();
                        context.Trackings.DeleteOnSubmit(res);
                    }

                    context.SubmitChanges();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
